package com.xidian.edu.cn.order.config;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.xidian.edu.cn.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117627992";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCBtiFeww76dafsZjTViN7UKU1cIT0GoGui0hkTMMCcQ18R5S4J3MGYueajCRnJN/AnX+LYF1xv5XIQ9YofiYwqQXVjrBY23BV2SgHJMtZJMSlWc9GgzvLXAN/+GkNdw07U+5+Kuo+vqUPzVFR8c8vfMtKmHYq6xZUJ0dAsLWDPeyQLD///K8dB+UMrXyzD0jm0ToSY2BUuIlClW62iOS3e5RzP44ko8T9nh2PlIqCttlN1VsR60dwHkrT/sq2PtpiFwPv8eK8tkvujvhTP4IkBSbv4lI9B9YRAC+XQbJB1sEG9vuqiWGMmuvZFqN/s7Uh5WBhLjCr8biFa0h1cxTDdAgMBAAECggEAUS/qWyHD8khKPtEKKM1ui2T+ore7NraaKGNQho5xQF161/yWgF3IXwucrFc56JQ2oK6HkbGtjewTJUJKncq+DITEMQq5LMU8ObnAvGoxbcZzlUUQ5JIjtSc7DBTsLXfdk7ma9n4nCU/s1BTofK0N4joxVwAOoTCiRG8q2Tvd6H7JPAVyLUps5EvsFedB3kdUaDcgmq08RP6Ff+kVNcMB7fbhq/IN14gy8XVW/IlcdISSo01cS5J0Imo3/4RHw2Shv2+WXPWg+kMTGY/OSAlQRjHo2rEscLLRUcrsBbGHq8b9j7VAwl00q9t1oyKSnKXp6s0RJcOWXQr8WHF+VmQvgQKBgQDMsBUoMlIW0sx3h5yjrRhVVplJdCWger8CTjy7tATLsjmXw+EsBi1XYvK1JcMVUDbzUeZmH0OL2DXqVtTw6KR2Q/PQv6dEBZqqIHZNt0HITZauKVgsjNp9c8DG9bLAOvxqQPQLzSlhOoMhBrK0TotpbxFoRdvqzdtvZICym9G7aQKBgQCiOmkq6U4OGuSy3VRl9KTBmEZpyesnhlyGfiwrLcj+I9PwEo0mOzd4uHcsTUnFJNL7vH1ghg0t4Ty5OW17GcZ8zIo0q5sWCBD/rcAAqa9mwwNqy2dgxgkQtGbkvFXtGpU6bPkZjJPkCOzbxSnonyckKNAPygvXO1In8WOF6iZfVQKBgFRVcEIhiYTx0WCmNTmSxeXTSlDjU+Vs7WoLxiYoI5mxkt0ezZWP7fqFQC62okwTwrA53E3TL8dci8pDgHTJxYkcskYd0NLL5E+YFGYszSO0JBMZ4cRpPFRX7uOKs91T8E/u9x4tp5DxxLlEwzha7EHCi0+FCPkTXAfnRsdQP/eRAoGAXgJvU1ijzPS0txofgH3GxBFULJXAWcU49Q9E1TjREsT8WlDFzvPd720mUlvvdfki8pKGLVtBY+b0Y3nuAbosFcIKDP+s/ZVrFF9BtqCebnRwFIEOxTu9oRqJzd9+A8q104e2yqF2Sl0JtUmKQa2I5yFngN7bAbz0nQT7YW0RE4kCgYBad9g3NLKrotjdlZP0ONh2cZqO/n41CnRAI8c1WFdDwU2BvBCiRa2dc5vzBrZRlcLjQZvhwjFbwD1t4UzRMjIgoXqv9Evpnl4cMT1XmMG14YxnZSo2obCx5fqNxW30D+PgUZCxhY2TyIUdvRUFgech32pM/YnRuTO6K7NiMAKoFg==";;
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3/bsogiELrtx5NR4rz8eXlP7H+DV/nCm/U7Iovw6V2RBEKxgv3m4eNiPs4wvZQku5pd1xaEsJhBOOwYZAp+MW/ZA+NK+yPywNRxpRDDVFpkQEa1dUsK8KO2SOlU0fx5CLFyMPbd3pQ+GYMXbmlxjB0qiGrt03Dcl7TBA5uM/ukJpE7v1a/err9irOdUiZ8lcau8LmvXyhYOUVTQdrjtTb7u+PCdEaT4rN2OPabhkVBu/kF8uDqSCCtBVCLI5ZikETEHmOk9nlxQ+/UoX959ClCxfR14eBHwOjPefFeHAITMx/V7vGb3a2f3mFGbM3DqWAMLmYcmFvvpg6Lkzlup+3QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://497n86m7k7.52http.net/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //自动收单时间设置为30m
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
